<?php
declare(strict_types=1);
/**
 * Created by project.
 * Act23: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */
namespace App\Controller;

use Hyperf\Contract\SessionInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;

use App\Libs\ReMsg;
use App\Libs\DiyError;
use App\Libs\Wx\WxSdkEasy;

#[Controller(prefix: 'wxTools')]
class WxApiAuthController extends AbstractController
{
    #[Inject] protected SessionInterface $session;
    #[Inject] protected ReMsg $reMsg;
    #[Inject] protected DiyError $diyError;
    #[Inject] protected WxSdkEasy $wxEasySdk;

    #[RequestMapping(path: 'wxAuth',methods: 'get')]
    public function wxAuth()
    {
        try{
            $code = $this->request->query('code');
            if ($code == null) {
                //检查callback地址
                $backUrl = $this->request->query('page_url', null);
                if (empty($backUrl)) return $this->reMsg->re(10003);
                //保存callback地址
                $this->session->set('backUrl', $backUrl);
                $this->session->save();
                //授权地址
                $now = $this->request->fullUrl();
                //域名更换
                $now = str_replace(config('app_domain'), config('app_host'), $now);
                return $this->response->redirect($this->wxEasySdk->authWeb($now));
            }else {
                //检查callback地址
                $backUrl = $this->request->query('page_url');
                if (empty($backUrl)) return $this->reMsg->re(10003);
                //获取session的backUrl
                $backUrlSession = $this->session->get('backUrl');
                if($backUrl!=$backUrlSession) {
//                    return $this->reMsg->re(10003,'点击底部【使用完整服务】完成微信授权');
                    //授权之前需要点击 【使用完成服务】
                    return $this->response->redirect($backUrl);
                }
                try {
                    $wxUserInfo = $this->wxEasySdk->userInfo($code)->getRaw();
                } catch (\Exception $exception) {
                    return $this->reMsg->re(50002,$exception->getMessage());
                }
                if(empty($wxUserInfo) || $wxUserInfo['openid']==null) return $this->reMsg->re(50002,'wechat info loss');
                //定义对象
                $api_user = new \stdClass();
                $api_user->openid = $wxUserInfo['openid'];
                $api_user->nickname = $wxUserInfo['nickname'];
                $api_user->avatar = $wxUserInfo['headimgurl'];
//                $api_user->sex = $wxUserInfo['sex'];
//                $api_user->province_city = $wxUserInfo['province'].$wxUserInfo['city'];
                $api_user->app_env = config('app_name').'-'.config('app_env');//运行环境配置
                $this->session->set('api_user', $api_user);
                $this->session->save();
                return $this->response->redirect($backUrl);
            }
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }
}
